<?php
namespace app\backstage\logic;
use think\Db;
class CostManual
{
    /*列表数据获取*/
    public function getList($params)
    {
        $field = 'id,type,cost_date,money,channel_id,advert_media_id,link_id';


        if( !(isset($params['channel_id']) && $params['channel_id'] > 0) )
        {
            if (request()->_role->alias == 'agent') {
                $channel_ids = Db::table('cms_manager_user')->where('parent_id','=',request()->_user->id)
                    ->column('id');
                $params['channel_id'] = $channel_ids ? $channel_ids : [0];
            }else{
                $params['channel_id'] = request()->_user->id;
            }
        }

        $condition[] = ['channel_id', 'in', 'not_empty','channel_id'];
        $condition[] = ['type', 'eq', 'gt:0','type'];
        $condition[] = ['cost_date', 'eq', 'require','cost_date'];

        $where   = buildQueryCondition($condition, $params);

        if( isset($params['field']) ){
            $orderBy = [ $params['field'] => $params['order'] ];
        }else{
            $orderBy = ['cost_date' => 'DESC'];
        }

        $CostManual = \think\facade\App::model('CostManual');
        $list = $CostManual->getList($where, $field, $params['page'], $params['limit'], $orderBy);

        if($list){

            $channel_id_gh = array_unique(array_column($list,'channel_id'));
            $channels = Db::table('cms_manager_user')->where('id','in',$channel_id_gh)->column('id,account');

            $link_id_gh = array_unique(array_column($list,'link_id'));
            $links = Db::table('quick_link')->where('id','in',$link_id_gh)->column('id,name');

            $type = [1=>'投放号',2=>'投放媒体',3=>'推广链接'];

            foreach($list as $k=>$v)
            {
                $list[$k]['channel_name'] = isset($channels[$v['channel_id']]) ? $channels[$v['channel_id']] : '';
                $list[$k]['media_name'] = getAdvertMediaName($v['advert_media_id']);
                $list[$k]['link_name'] = isset($links[$v['link_id']]) ? $links[$v['link_id']] : '';
                $list[$k]['money'] = getQuotient($v['money'],100);
                $list[$k]['type_name'] = $type[$v['type']];
            }
        }

        $result['count']    = $CostManual->getTotalNumber($where);
        $result['list']     = $list;
        return $result;
    }

    /*数据获取*/
    public function getInfo($where)
    {
        $field = 'id,type,channel_id,link_id,money,advert_media_id,cost_date';
        $AdvertUser = \think\facade\App::model('CostManual');

        $info = $AdvertUser->getOne($where,$field);

        return $info;
    }

    /*数据添加*/
    public function addInfo($data)
    {
        $CostManual = \think\facade\App::model('CostManual');
        $res = $CostManual->insertRecord($data);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*数据编辑*/
    public function editInfo($where,$data)
    {
        $CostManual = \think\facade\App::model('CostManual');
        $res = $CostManual->updateRecord($where,$data);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*数据删除*/
    public function delInfo($where)
    {
        $CostManual = \think\facade\App::model('CostManual');
        $res = $CostManual->deleteRecord($where);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*数据处理*/
    public function dataProcessing($params)
    {

        $agent_id = Db::table('cms_manager_user')->where('id',$params['channel_id'])->value('parent_id');

        $data['type']               = $params['type'];
        $data['agent_id']           = $agent_id;
        $data['link_id']            = $params['type'] == 3 ? $params['link_id'] : 0;
        $data['channel_id']         = $params['channel_id'];
        $data['money']              = $params['money']*100;
        $data['advert_media_id']    = $params['type'] != 1 ? $params['advert_media_id'] : 0;
        $data['cost_date']          = $params['cost_date'];

        $record = Db::table('quick_link')->where('id',$params['link_id'])->field('quick_id,novel_id')->findOrEmpty();

        $record_quick = Db::table('quick_app_mp_manager')->where('uid',$params['channel_id'])->field('quick_id')->findOrEmpty();

        $data['quick_id']   = $record_quick ? $record_quick['quick_id'] : 0;
        $data['nid']        = $record ? $record['novel_id'] : 0;

        return ['data'=>$data];
    }

}